/******************************************************************************
    myBoard is a port of MacOS X dashboard application for windows.
    Copyright (C) 2007,2008  by Yann ALET
    Email : yann.alet@gmail.com

    myBoard is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    myBoard is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
****************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Diagnostics;
using System.ComponentModel;
using System.Threading;

namespace myBoard.Framework
{
    /// <summary>
    /// Interaction logic for ImageLoader.xaml
    /// </summary>

    public partial class ImageLoader : System.Windows.Controls.UserControl
    {
        Storyboard _showImageStoryboard;
        BackgroundWorker _bWorker;
        public static ImageLoader _this;


        bool _imageDownloadedOrLoaded;

        public Uri Source
        {
            get
            {
                return GetValue(SourceProperty) as Uri;
            }
            set
            {
                SetValue(SourceProperty, value);
            }
        }

        public static readonly DependencyProperty SourceProperty = DependencyProperty.Register("Source", typeof(Uri), typeof(ImageLoader),
            new PropertyMetadata(null, new PropertyChangedCallback(OnSourceChanged)));

        static void OnSourceChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null)
            {
                _this._loader.ShowLoader();
                _this._bWorker.RunWorkerAsync(e.NewValue as Uri);
            }
        }

        public ImageLoader()
        {
            InitializeComponent();
            _this = this;
            _showImageStoryboard = this.FindResource("ShowImage") as Storyboard;
            _bWorker = new BackgroundWorker();
            _bWorker.WorkerReportsProgress = true;
            _bWorker.DoWork += new DoWorkEventHandler(StartImageLoading);
            _bWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(_bWorker_RunWorkerCompleted);
            _bWorker.ProgressChanged += new ProgressChangedEventHandler(_bWorker_ProgressChanged);
        }

        void _bWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            _loadingImage.Source = (BitmapImage) e.Result;
            _loader.CloseLoader();
            this.BeginStoryboard(_showImageStoryboard);
        }

        void _bWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            
        }


        public void StartImageLoading(object sender, DoWorkEventArgs e)
        {
            Uri source = e.Argument as Uri;
            if (source != null)
            {
                _imageDownloadedOrLoaded = false;
                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.DownloadCompleted += new EventHandler(bitmapImage_DownloadCompleted);
                bitmapImage.DownloadProgress += new EventHandler<DownloadProgressEventArgs>(bitmapImage_DownloadProgress);
                bitmapImage.BeginInit();
                bitmapImage.UriSource = source;
                bitmapImage.EndInit();

                if (!bitmapImage.IsDownloading)
                    bitmapImage_DownloadCompleted(null, null);

                while (!_imageDownloadedOrLoaded)
                    continue;
                
            }
        }

        void bitmapImage_DownloadProgress(object sender, DownloadProgressEventArgs e)
        {
            Debug.WriteLine(e.Progress);
            _bWorker.ReportProgress(e.Progress);
        }

        void bitmapImage_DownloadCompleted(object sender, EventArgs e)
        {
            //Debug.WriteLine(string.Format("{0}  -  {1}", DateTime.Now.Ticks, "Downloaded"));
            _imageDownloadedOrLoaded = true;
        }        
    }
}